package org.korecky.gnotedigitalorganizer.panels;

import java.awt.BorderLayout;
import java.io.File;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.korecky.gnotedigitalorganizer.AppContext;
import org.korecky.gnotedigitalorganizer.components.FileTreeCellRenderer;

public class LocalLibraryPanel extends JPanel {
    private AppContext appContext;
    private JToolBar toolBar;
    private JButton btnImport;
    private JButton btnExport;
    private JButton btnNewFolder;
    private JButton btnDelete;
    private JTree tree;
    private JPanel panel_2;
    private JScrollPane scrollPane;
    private JPanel panel;

    public LocalLibraryPanel() {
	this(null);
    }

    /**
     * Create the panel.
     */
    public LocalLibraryPanel(AppContext appContext) {
	this.appContext = appContext;
	initComponents();
	ResetPanel();
    }

    private void ResetPanel() {
	if ((this.appContext != null) && (this.appContext.getAppSettings() != null)) {
	    String libraryPath = this.appContext.getAppSettings().getLibraryPath();
	    if (libraryPath != null) {
		File libraryDirectory = new File(libraryPath);
		if ((libraryDirectory.exists()) && (libraryDirectory.isDirectory())) {
		    // Load content of G-Note library
		    LoadDirectoryContent(libraryDirectory, null, null);
		}
	    }
	}
    }

    private void LoadDirectoryContent(File directory, DefaultMutableTreeNode parentNode, DefaultTreeModel model) {
	DefaultMutableTreeNode directoryNode = new DefaultMutableTreeNode(directory);
	directoryNode.setAllowsChildren(true);
	if (parentNode == null) {
	    model = new DefaultTreeModel(directoryNode, true);
	    tree.setModel(model);
	}
	else
	    model.insertNodeInto(directoryNode, parentNode, parentNode.getChildCount());

	if ((directory.exists()) && (directory.isDirectory())) {
	    for (File file : directory.listFiles()) {
		if (file.isDirectory())
		    LoadDirectoryContent(file, directoryNode, model);
		else {
		    DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(file);
		    fileNode.setAllowsChildren(false);
		    model.insertNodeInto(fileNode, directoryNode, directoryNode.getChildCount());
		}
	    }
	}
    }

    private void initComponents() {
	setLayout(new BorderLayout(0, 0));

	this.toolBar = new JToolBar();
	this.toolBar.setToolTipText("Import");
	add(this.toolBar, BorderLayout.NORTH);

	this.btnNewFolder = new JButton("");
	this.btnNewFolder.setToolTipText("New folder");
	this.btnNewFolder.setIcon(new ImageIcon(LocalLibraryPanel.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons16x16/folder-new.png")));
	this.toolBar.add(this.btnNewFolder);

	this.btnDelete = new JButton("");
	this.btnDelete.setToolTipText("Delete");
	this.btnDelete.setIcon(new ImageIcon(LocalLibraryPanel.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons16x16/edit-delete.png")));
	this.toolBar.add(this.btnDelete);
	this.toolBar.addSeparator();
	this.btnImport = new JButton("");
	this.btnImport.setToolTipText("Import");
	this.btnImport.setIcon(new ImageIcon(LocalLibraryPanel.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons16x16/document-import.png")));
	this.toolBar.add(this.btnImport);

	this.btnExport = new JButton("");
	this.btnExport.setIcon(new ImageIcon(LocalLibraryPanel.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons16x16/document-export.png")));
	this.btnExport.setToolTipText("Export");
	this.toolBar.add(this.btnExport);

	this.panel_2 = new JPanel();
	add(this.panel_2, BorderLayout.CENTER);

	this.tree = new JTree();
	this.tree.setCellRenderer(new FileTreeCellRenderer());

	this.scrollPane = new JScrollPane();
	GroupLayout gl_panel_2 = new GroupLayout(this.panel_2);
	gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING).addGroup(gl_panel_2.createSequentialGroup().addContainerGap().addComponent(this.tree, GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE).addPreferredGap(ComponentPlacement.RELATED).addComponent(this.scrollPane, GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE).addContainerGap()));
	gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING).addGroup(gl_panel_2.createSequentialGroup().addContainerGap().addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING).addComponent(this.scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE).addComponent(this.tree, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)).addContainerGap()));

	this.panel = new JPanel();
	this.scrollPane.setViewportView(this.panel);
	this.panel_2.setLayout(gl_panel_2);
    }
}
